package com.hainatong.jcmvvmbase.mainhome.fragment.warehouse;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.hainatong.jcmvvmbase.MvvmLazyFragment;
import com.hainatong.jcmvvmbase.R;
import com.hainatong.jcmvvmbase.databinding.FragmentBrokenSubmitBinding;
import com.hainatong.jcmvvmbase.pictureselector.PictureBean;
import com.hainatong.jcmvvmbase.pictureselector.PictureSelector;
import com.hainatong.mvvmbase.base.NoViewModel;

import java.io.File;

public class BrokenSubmitFragment extends MvvmLazyFragment<FragmentBrokenSubmitBinding, NoViewModel>
{
    private static String BROKEN_SUB_TAG = "BrokenSubmitFragment";

    private String goodsCode ;

    public static BrokenSubmitFragment newInstance(String scanCode)
    {
        Bundle bundle = new Bundle();
        bundle.putString( BROKEN_SUB_TAG , scanCode );
        BrokenSubmitFragment brokenFragment = new BrokenSubmitFragment();
        brokenFragment.setArguments( bundle );
        return brokenFragment;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_broken_submit;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    @Override
    protected void initParameters()
    {
        Bundle args = getArguments();
        if(  args != null )
        {
            goodsCode = args.getString(BROKEN_SUB_TAG , "");
        }
    }

    @Override
    protected void initView(View view)
    {
        viewDataBinding.ivPhotoBtn.setOnClickListener( v -> {

            //选择图片
            PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE)
                    .selectPicture(false);

        } );
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        /* 选择图片 结果回调*/
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE)
        {
            if (data != null)
            {
                PictureBean pictureBean = data.getParcelableExtra(PictureSelector.PICTURE_RESULT);
                Log.e("PictureSelector","是否裁剪: " + pictureBean.isCut());
                Log.e("PictureSelector","原图地址: " + pictureBean.getPath());
                Log.e("PictureSelector","图片 Uri: " + pictureBean.getUri());

                File imgFile = new File( pictureBean.getPath() );


                if (false)
                {
                    //BitmapFactory.decodeFile(pictureBean.getPath())

                    //图片文件
                    //File file = new File(pictureBean.getPath());

                }
                else
                {
                    //HeadImg.setImageURI(pictureBean.getUri());
                }

                //使用 Glide 加载图片
                Glide.with(this)
                        .load(pictureBean.isCut() ? pictureBean.getPath() : pictureBean.getUri())
                        .apply(RequestOptions.centerCropTransform()).into(viewDataBinding.ivPhotoBtn);

                //实际开发中将图片上传到服务器成功后需要删除全部缓存图片（即裁剪后的无用图片）
                //FileUtils.deleteAllCacheImage(this);
            }
        }
    }

}
